package com.dt.dubai.core.email;

import com.dt.dubai.core.email.bean.MailBean;
import com.dt.dubai.core.email.service.ser.MailService;

import com.dt.dubai.core.utile.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/email")
public class MailController {
    private static final Logger logger = LoggerFactory.getLogger(MailController.class);
    @Autowired
    private MailService mailService;

    @GetMapping("/textMail")
    @ResponseBody
    public Result sendTextMail(MailBean mailBeanDo){
        try {
            mailService.sendTextMail(mailBeanDo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return  Result.error();

        }
        return Result.ok();
    }

    @GetMapping("/htmlMail")
    @ResponseBody
    public Result sendHtmlMail(MailBean mailBean){
        /*mailBean.setTo("huangxiaoyan9724@163.com");
        mailBean.setContent("<!doctype html>\n" +
                "<html lang=\"zh-cmn-Hans\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <meta name=\"renderer\" content=\"webkit\" />\n" +
                "    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\" />\n" +
                "    <title>Document</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <div class=\"container\"\n" +
                "         style=\" width: 800px;\n" +
                "         height: auto;\n" +
                "         margin: 0 auto;\n" +
                "         border: 1px #dddddd solid;\n" +
                "         border-top: 4px #3498db solid;\n" +
                "         font: 14px Microsoft Yahei;\n" +
                "         color: #333;\">\n" +
                "        <div class=\"main\" style=\"padding: 0 15px;\">\n" +
                "            <div style=\"\n" +
                "                 border: 1px #f39d12 dashed;\n" +
                "                 background-color: #fffdf4;\n" +
                "                 margin: 20px 0;\n" +
                "                 border-radius: 6px;\">\n" +
                "                <table width=\"100%\" style=\"border: none\">\n" +
                "                    <tr>\n" +
                "                        <td style=\"width: 20%; text-align: center; padding-top: 25px\">\n" +
                "                            <img src=\"#\" alt=\"\">\n" +
                "                        </td>\n" +
                "                        <td colspan=\"2\" style=\"font-size: 18px; line-height: 1.6;padding-top: 30px\">\n" +
                "                            <div style=\"text-indent: 36px; padding-right: 24px\">\n" +
                "                                 你好，世界。\n" +
                "                            </div>\n" +
                "                        </td>\n" +
                "                    </tr>\n" +
                "                </table>\n" +
                "            </div>\n" +
                "        </div>\n" +
                "        <div class=\"footer\" style=\"margin: auto;\n" +
                "                            padding: 15px 0 15px 15px;\n" +
                "                            background-color: #fafafa;\n" +
                "                            border-top: 1px #ddd solid;\n" +
                "                            color: #333;\n" +
                "                            height: auto;\n" +
                "                            zoom: 1;\n" +
                "                            overflow: auto;\">\n" +
                "            <table style=\"width: 100%\">\n" +
                "                <tr>\n" +
                "                    <td style=\"width: 60%\">\n" +
                "                        <h4 style=\"font-size: 16px;\n" +
                "                             margin: 10px 0;\">\n" +
                "                          迅搭云\n" +
                "                        </h4>\n" +
                "                        <p>\n" +
                "                            公司地址：\n" +
                "                            <b>宇宙银河系太阳系地球村88号</b>\n" +
                "                        </p>\n" +
                "                        <p>\n" +
                "                            官方网站：\n" +
                "                            <a href=\"http://xundacloud.com/#/home\" style=\"text-decoration: none; color: #333; font-weight: bold;\">http://xundacloud.com/#/home</a>\n" +
                "                        </p>\n" +
                "                        <p>\n" +
                "                            服务热线：\n" +
                "                            <b>\n" +
                "                                <span style=\"display: inline-block; margin-right: 16px;\">010-82000</span>\n" +
                "                            </b>\n" +
                "                        </p>\n" +
                "                    </td>\n" +
                "                    <td style=\"text-align: right;\">\n" +
                "                        <div style=\" display: inline-block;width: 120px ; padding-top: 10px; padding-right: 200px;\">\n" +
                "                            <img src=\"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1906469856,4113625838&fm=26&gp=0.jpg\" alt=\"\" style=\"vertical-align: top\">\n" +
                "                        </div>\n" +
                "                    </td>\n" +
                "                </tr>\n" +
                "            </table>\n" +
                "        </div>\n" +
                "    </div>\n" +
                "</body>\n" +
                "</html> ");*/
        /*mailBean.setTitle("您有一条新消息");
        mailBean.setContentType("html");*/

        try {
            Map<String,Object> map = new HashMap<>();
            mailBean.setMaps(map);
            mailService.sendHtmlMail(mailBean,true);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return  Result.error();
        }
        return Result.ok();
    }

    @GetMapping("/templateMail")
    @ResponseBody
    public Result sendTemplateMail(MailBean mailBean) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("username", "我变大了");
            mailBean.setMaps(map);
            mailService.sendTemplateMail(mailBean);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return  Result.error();
        }
        return Result.ok();
    }
}
